package com.shengde.jinwawaforleader.model.homePage;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.base.BaseActivity;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProSHowGetNote;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 填写验证码页面
 * Created by Administrator on 2016/11/24 0024.
 */
public class WriteAuthCodeActivity extends BaseActivity {

	private static final String TAG = "WriteAuthCodeActivity";
	private static final int FINSH = 1;
	private EditText edt_num;
	private Button btn_next;
	private Button btn_repeat_send;
	private TextView tv_title;
	private TextView tv_telephone;
	private TextView tv_back;
	private int recLen = 60;
	private Timer timer;
	private TimerTask timerTask;
	private String telephoneNum;
	private String mCheckSUM;


	@Override
	public void onRoot(Bundle savedInstanceState) {
		setContentView(R.layout.write_auth_code_activity);
	}

	@Override
	public void initView() {
		tv_back = (TextView) findViewById(R.id.tv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_telephone = (TextView) findViewById(R.id.tv_telephone);
		btn_next = (Button) findViewById(R.id.btn_next);
		edt_num = (EditText) findViewById(R.id.edt_num);
		btn_repeat_send = (Button) findViewById(R.id.btn_repeat_send);

	}

	@Override
	public void initData() {
		tv_title.setText(getString(R.string.auth_code));
		telephoneNum = getIntent().getStringExtra("telephone_num");
		tv_telephone.setText(telephoneNum);
		showGetNote(telephoneNum);
		showTime();


	}

	/**
	 * 设置倒计时时间方法
	 */
	private void showTime() {
		btn_repeat_send.setText(recLen + "");
		if (timer == null) {
			timer = new Timer();
			startTime();
		} else {
			startTime();
		}

	}

	/**
	 * 开始倒计时时间
	 */
	private void startTime() {
		if (timerTask != null) {
			timerTask.cancel();
		}
		timerTask = new TimerTask() {

			@Override
			public void run() {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						btn_repeat_send.setClickable(false);
						recLen--;
						btn_repeat_send.setText(recLen + "");
						if (recLen < 0) {
							btn_repeat_send.setText(getString(R.string.repeat_send));
							btn_repeat_send.setClickable(true);
						}
					}
				});
			}
		};

		timer.schedule(timerTask, 1000, 1000);
	}

	@Override
	public void initListener() {
		tv_back.setOnClickListener(this);
		btn_next.setOnClickListener(this);
		btn_repeat_send.setOnClickListener(this);
	}

	@Override
	public void onWidgetClick(View view) {
		switch (view.getId()) {
			case R.id.tv_back:
				finish();
				break;
			case R.id.btn_next:
				if (TextUtils.isEmpty(edt_num.getText().toString().toString())) {
					Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
				} else if (edt_num.getText().toString().trim().equals(mCheckSUM)){

					Intent intent = new Intent(this, SettingPasswordActivity.class);
					intent.putExtra("telephoneNum",telephoneNum);
					startActivity(intent);
				}else {
					Toast.makeText(this, "验证码不正确", Toast.LENGTH_SHORT).show();
				}
				break;
			case R.id.btn_repeat_send:
				showGetNote(telephoneNum);
				recLen = 60;
				showTime();
				break;
		}
	}

	private void showGetNote(String telephoneNum) {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/sms/ValidatePhone";
		} else {
			url = BaseProtocol.HOST_BASE + "/sms/ValidatePhone";
		}
		RequestParams params = BaseProtocol.getRequestParams(url);
		params.addParameter("phone", telephoneNum);
		params.addParameter("type", 2);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProSHowGetNote proSHowGetNote = new Gson().fromJson(result, ProSHowGetNote.class);
				Log.i(TAG, "onSuccess: code  = " + proSHowGetNote.getCode());
				if (proSHowGetNote.getCode() == 0) {
					Log.i(TAG, "onSuccess: 获取验证码成功");
					mCheckSUM = proSHowGetNote.getChecksum();
				}else {
					Toast.makeText(WriteAuthCodeActivity.this, "获取验证码失败", Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}
}
